/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.indicator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.gitee.kinbug.sniper.common.entity.Bar;

/**
 * -移动平均值EMA
 * @ClassName: EMAIndicator
 * @author kinbug
 * @date 2020-11-20
 */
public class SMAIndicator {

	/**
	 * 移动平均数
	 * 算法：多数想加，求平均值
	 * @param list 均数源数集合
	 */
	public static BigDecimal getValue(List<BigDecimal> list, int cycle) {
		BigDecimal sum = list.stream().reduce((x, y) -> x.add(y)).get();
		return sum.divide(new BigDecimal(cycle), 4, RoundingMode.DOWN);
	}
	
	
	/**
	 * 移动平均数
	 * 算法：多数想加，求平均值
	 * @param list 均数源数集合
	 */
	public static BigDecimal getByBarValue(List<Bar> list, int cycle) {
		List<Bar> subList = list.subList(Math.max(list.size() - cycle, 0), list.size());
		BigDecimal sum = subList.stream().map(b -> b.getClose()).reduce((x, y) -> x.add(y)).get();
		return sum.divide(new BigDecimal(cycle), 4, RoundingMode.DOWN);
	}
}
